package com.gable.exhibition.service.impl;

import com.gable.common.util.DateUtils;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.*;
import com.gable.exhibition.model.*;
import com.gable.exhibition.service.DeviceService;
import com.gable.exhibition.service.InstallService;
import com.gable.exhibition.thread.CheckSendSmsThread;
import com.gable.exhibition.thread.InstallSendSmsThread;
import com.gable.exhibition.util.ExceptionUtil;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/2/7.
 */
@Service
public class InstallServiceImpl implements InstallService {

    @Value("${remindDay}")
    Integer remindDay;

    @Value("${endInstallRemindByDoctor}")
    String endInstallRemindByDoctor;        //安装完成推送给医院

    @Value("${acceptanceByService}")
    String acceptanceByService;     //验收完成推送给给服务商

    @Autowired
    InstallDao installDao;

    @Autowired
    ContractDeviceDao contractDeviceDao;

    @Autowired
    DeviceDao deviceDao;

    @Autowired
    ContractDao contractDao;

    @Autowired
    ContractDeviceActualSendDao contractDeviceActualSendDao;

    @Autowired
    DeviceService deviceService;

    @Autowired
    EventDao eventDao;

    @Autowired
    FileDao fileDao;

    @Autowired
    UserDao userDao;

    @Autowired
    ContractActualSendDao contractActualSendDao;

    @Autowired
    AppointDao appointDao;

    @Override
    public ReturnResultUtil queryRemind(ParameterView parameterView) {
        Map<String,Object> map = new HashMap<String,Object>();
        Integer count = installDao.queryInstallRemindCount(parameterView);
        if(count > 0){
            List<InstallView> installViews = installDao.queryInstallRemind(parameterView);
            map.put("data",installViews);
        }

        map.put("count",count);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,map);
    }

    @Override
    public ReturnResultUtil installList(ParameterView parameterView) {
        Map<String,Object> map = new HashMap<String,Object>();
        Integer count = installDao.installListCount(parameterView);
        if(count > 0){
            List<InstallView> installViews = installDao.installList(parameterView);
            map.put("data",installViews);
        }
        map.put("count",count);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,map);
    }

    @Override
    public ReturnResultUtil installInfo(ParameterView parameterView) {
        InstallView installView = installDao.installInfo(parameterView);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,installView);
    }

    @Override
    @Transactional
    public ReturnResultUtil doInstall(InstallView installView) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        Long contractDeviceActualSendId = installView.getContractDeviceActualSendId();
        ContractDeviceActualSend contractDeviceActualSend = contractDeviceActualSendDao.selectByPrimaryKey(contractDeviceActualSendId);
        if(contractDeviceActualSend == null)
            return new ReturnResultUtil(500,MessageUtil.SENDDEVICEISNULL);

        Long contractDeviceId = contractDeviceActualSend.getContractDeviceId();
        ContractDevice contractDevice = contractDeviceDao.selectByPrimaryKey(contractDeviceId);
        if(contractDevice == null)
            return new ReturnResultUtil(500,MessageUtil.CONTRACTDEVICEISNULL);

        ContractView contract = contractDao.queryContractForSendSms(contractDevice.getContractId());
        if(contract == null)
            throw new ExceptionUtil(500,MessageUtil.CONTRACTNOEXIST);

        DeviceView device = new DeviceView();
        device.setDeviceName(contractDevice.getDeviceName());
        device.setDeviceModel(contractDevice.getDeviceModel());
        device.setPlaceOrigin(contractDevice.getPlaceOrigin());
        device.setQuantity(1);
        device.setSpecifications(contractDevice.getSpecifications());
        device.setPrice(contractDevice.getPrice());
        device.setAttachment(contractDevice.getAttachment());
        device.setUpkeepCycle(contractDevice.getUpkeepCycle());
        device.setSn(installView.getSn());
        device.setInstallDate(installView.getActualInstallDate());
        device.setPurchaseDate(contract.getSignDate());
        device.setManufactureDate(contractDeviceActualSend.getActualSendDate());
        device.setDeviceStatus(1500L);
        device.setSupplierId(contract.getSecondParty());
        device.setManufacturerId(contractDevice.getManufacturerId());
        device.setMaintainerId(contractDevice.getMaintainerId());
        device.setHospitalId(contract.getFirstParty());
        device.setContractDeviceId(contractDevice.getContractDeviceId());
        device.setCreatePerson(installView.getUserId());
        device.setIsDel(1);
        ReturnResultUtil resultUtil = deviceService.addOrUpdateDevice(device);
        if(resultUtil.getRet() != 200)
            throw new ExceptionUtil(500,resultUtil.getMsg(),device);

        //添加设备安装事件
        Event event = new Event();
        event.setEventId(UUIDGenerator.getUniqueLong());
        event.setKeyType(1603L);        //设备事件
        event.setKeyId(device.getDeviceId());
        event.setEventTitle("设备安装");
        event.setEventContent("设备于"+format+"安装");
        event.setEventDateTime(format);
        event.setType(1702L);   //设备安装	设备事件类型
        event.setCreatePerson(installView.getUserId());
        int row = eventDao.insertSelective(event);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);

        //添加安装完成事件
        event.setEventId(UUIDGenerator.getUniqueLong());
        event.setKeyType(1600L);        //安装事件
        event.setKeyId(installView.getInstallId());
        event.setEventTitle("设备安装完成");
        event.setEventContent("设备于"+format+"安装完成，等待验收");
        event.setEventDateTime(format);
        event.setType(2401L);   //设备安装	设备事件类型
        event.setCreatePerson(installView.getUserId());
        row = eventDao.insertSelective(event);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);

        installView.setStatus(1202L);   //待验收
        installView.setDeviceId(device.getDeviceId());
        row = installDao.updateByPrimaryKeySelective(installView);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSTALLIDFAILED,installView);
        List<String> fileIds = installView.getFileIds();
        if(fileIds != null && fileIds.size() > 0){
            for(String fileId : fileIds){
                File file = new File();
                file.setFileId(Long.parseLong(fileId));
                file.setKeyId(installView.getInstallId());
                row = fileDao.updateByPrimaryKeySelective(file);
                if(row == 0)
                    throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,file);
            }
        }

        FileView fileView = new FileView();
        fileView.setFileType("3200");
        fileView.setKeyId(installView.getInstallId());
        fileView.setKeyType(1404L);
        List<FileView> fileViews = fileDao.queryFiles(fileView);
        for(FileView file : fileViews){
            file.setFileId(UUIDGenerator.getUniqueLong());
            file.setKeyId(device.getDeviceId());
            file.setKeyType(1401L);
            file.setFileTypeName("3406");
            int i = fileDao.insertSelective(file);
            if(i == 0)
                throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,file);
        }

        String serviceProviderName = contract.getSupplierName();   //服务商名称
        String deviceName = contractDevice.getDeviceName();          //设备名称
        //format;     //安装日期
        List<String> hospitalMobile = contract.getMobileList();      //医院管理员电话
        //sendSmsForInstallComplete  //安装完成推送给医院
        new Thread(new InstallSendSmsThread(serviceProviderName,deviceName,format,hospitalMobile,endInstallRemindByDoctor)).start();
        return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL);
    }

    @Override
    public ReturnResultUtil queryRemindForCheck(ParameterView parameterView) {
        Map<String,Object> map = new HashMap<String,Object>();

        Integer count = installDao.queryRemindForCheckCount(parameterView);
        if(count > 0){
            List<InstallView> installViews = installDao.queryRemindForCheck(parameterView);
            map.put("data",installViews);
        }
        map.put("count",count);

        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,map);
    }

    @Override
    @Transactional
    public synchronized ReturnResultUtil doCheck(InstallView installView){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long status = installView.getStatus();
        Long deviceStatus = status == 1203L?1500L:1503L;
        Integer isDel = status == 1203L?0:1;
        String format = simpleDateFormat.format(new Date());

        Long installId = installView.getInstallId();
        Install install = installDao.selectByPrimaryKey(installId);
        //虚拟安装单，需要录入实际设备表
        Event deviceEvent = new Event();    //设备事件
        deviceEvent.setEventId(UUIDGenerator.getUniqueLong());
        deviceEvent.setKeyType(1603L);
        deviceEvent.setEventDateTime(format);
        deviceEvent.setType(1702L);
        deviceEvent.setCreatePerson(installView.getUserId());
        if(install.getIsFictitious() == 1){
            Long contractDeviceActualSendId = installView.getContractDeviceActualSendId();
            ContractDeviceActualSend contractDeviceActualSend = contractDeviceActualSendDao.selectByPrimaryKey(contractDeviceActualSendId);
            if(contractDeviceActualSend == null)
                return new ReturnResultUtil(500,MessageUtil.SENDDEVICEISNULL);

            Long contractDeviceId = contractDeviceActualSend.getContractDeviceId();
            ContractDevice contractDevice = contractDeviceDao.selectByPrimaryKey(contractDeviceId);
            if(contractDevice == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTDEVICEISNULL);

            Contract contract = contractDao.selectByPrimaryKey(contractDevice.getContractId());
            if(contract == null)
                return new ReturnResultUtil(500,MessageUtil.CONTRACTNOEXIST);

            DeviceView device = new DeviceView();
            device.setDeviceName(contractDevice.getDeviceName());
            device.setDeviceModel(contractDevice.getDeviceModel());
            device.setPlaceOrigin(contractDevice.getPlaceOrigin());
            device.setQuantity(1);
            device.setSpecifications(contractDevice.getSpecifications());
            device.setPrice(contractDevice.getPrice());
            device.setAttachment(contractDevice.getAttachment());
            device.setUpkeepCycle(contractDevice.getUpkeepCycle());
            device.setSn(installView.getSn());
            device.setDepartment(installView.getDepartment());
            device.setAssetsNo(installView.getAssetsNo());
            device.setInstallDate(format);
            device.setAcceptanceDate(format);
            device.setPurchaseDate(contract.getSignDate());
            device.setManufactureDate(contractDeviceActualSend.getActualSendDate());
            device.setDeviceStatus(deviceStatus);
            device.setManufacturerId(contractDevice.getManufacturerId());
            device.setSupplierId(contract.getSecondParty());
            device.setMaintainerId(contractDevice.getMaintainerId());
            device.setHospitalId(contract.getFirstParty());
            device.setContractDeviceId(contractDevice.getContractDeviceId());
            device.setCreatePerson(installView.getUserId());
            device.setIsDel(isDel);
            device.setEnableTime(installView.getEnableTime());
            ReturnResultUtil resultUtil = deviceService.addOrUpdateDevice(device);
            if(resultUtil.getRet() != 200)
                throw new ExceptionUtil(500,resultUtil.getMsg(),device);
            installView.setDeviceId(device.getDeviceId());

            deviceEvent.setKeyId(device.getDeviceId());
            deviceEvent.setEventTitle("设备安装");
            deviceEvent.setEventContent("设备于"+format+"验收安装");
        }else{
            Long deviceId = install.getDeviceId();  //实际设备ID
            Device device = deviceDao.selectByPrimaryKey(deviceId);
            if(device == null)
                throw new ExceptionUtil(500,MessageUtil.DEVICEISNULL);
            //更新验收日期
            device.setAcceptanceDate(format);
            device.setDepartment(installView.getDepartment());
            device.setAssetsNo(installView.getAssetsNo());
            device.setEnableTime(installView.getEnableTime());
            device.setDeviceStatus( deviceStatus);
            device.setIsDel(isDel);
            int row = deviceDao.updateByPrimaryKeySelective(device);
            if(row == 0)
                throw new ExceptionUtil(500,MessageUtil.UPDATEACCEPTANCE);

            deviceEvent.setKeyId(deviceId);
            deviceEvent.setEventTitle("设备验收");
            deviceEvent.setEventContent("设备于"+format+"验收通过并评分");
        }

        installView.setUpdatePerson(installView.getUserId());
        installView.setUpdateTime(format);
        int row = installDao.updateByPrimaryKeySelective(installView);
        if(row == 0)
            return new ReturnResultUtil(500,MessageUtil.CHECKFAILED);

        List<String> fileIds = installView.getFileIds();
        if(fileIds != null && fileIds.size() > 0){
            for(String fileId : fileIds){
                File file = new File();
                file.setFileId(Long.parseLong(fileId));
                file.setKeyId(installView.getInstallId());
                row = fileDao.updateByPrimaryKeySelective(file);
                if(row == 0)
                    throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,file);
            }
        }

        //添加设备事件
        row = eventDao.insertSelective(deviceEvent);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,deviceEvent);

        //添加安装验收事件
        Event event = new Event();
        event.setEventId(UUIDGenerator.getUniqueLong());
        event.setKeyId(installView.getInstallId());
        event.setKeyType(1600L);
        event.setEventDateTime(format);
        event.setType(2402L);
        event.setCreatePerson(installView.getUserId());
        event.setEventTitle("医院验收通过");
        event.setEventContent("医院于"+format+"验收通过并评分");
        //验收未通过
        if(status.intValue() == 1204){
            event.setEventTitle("医院验收不通过");
            event.setEventContent("医院于"+format+"验收不通过，待平台联系医院");
        }
        row = eventDao.insertSelective(event);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);

        //回写验收数量
        Long contractDeviceActualSendId = install.getContractDeviceActualSendId();
        ContractDeviceActualSend contractDeviceActualSend = contractDeviceActualSendDao.selectByPrimaryKey(contractDeviceActualSendId);
        ContractDevice contractDevice = contractDeviceDao.selectByPrimaryKey(contractDeviceActualSend.getContractDeviceId());
        if(contractDevice == null)
            throw new ExceptionUtil(500,MessageUtil.CONTRACTDEVICEISNULL);
        contractDevice.setActualAcceptanceQuantity(contractDevice.getActualAcceptanceQuantity()+1);
        row = contractDeviceDao.updateByPrimaryKeySelective(contractDevice);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.CHECKUPDATE,installView);

        if(install.getIsFictitious() == 0){
            new Thread(new CheckSendSmsThread(installId,installDao,userDao,acceptanceByService)).start();
        }
        return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL);
    }

    @Override
    @Transactional
    public ReturnResultUtil saveInstallData(InstallView installView,User user) {
        DateFormat df  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        installView.setFrontStatus(1203L);  //验收完成
        installView.setStatus(1205L);
        int row = 0;
        List<String> fileIds = installView.getFileIds();
        if(fileIds != null && fileIds.size() > 0){
            for(String fileId : fileIds){
                File file = new File();
                file.setFileId(Long.parseLong(fileId));
                file.setKeyId(installView.getInstallId());
                row = fileDao.updateByPrimaryKeySelective(file);
                if(row == 0)
                    throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,file);
            }
        }
        //平台信息补全  安装事件
        String format = df.format(new Date());
        Event event = new Event();
        event.setEventId(UUIDGenerator.getUniqueLong());
        event.setKeyType(1600L);//安装
        event.setKeyId(installView.getInstallId());
        event.setEventDateTime(format);
        event.setEventContent( "平台的" + user.getRealName() + "于"+format+"填写安装信息");
        event.setEventTitle("安装信息补全");
        event.setCreatePerson(user.getUserId());
        row = eventDao.insertSelective(event);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);

        Long contractDeviceActualSendId = installView.getContractDeviceActualSendId();
        ContractDeviceActualSend contractDeviceActualSend = contractDeviceActualSendDao.selectByPrimaryKey(contractDeviceActualSendId);
        if(contractDeviceActualSend == null)
            throw new ExceptionUtil(500,MessageUtil.SENDDEVICEISNULL);

        Long contractDeviceId = contractDeviceActualSend.getContractDeviceId();
        ContractDevice contractDevice = contractDeviceDao.selectByPrimaryKey(contractDeviceId);
        if(contractDevice == null)
            throw new ExceptionUtil(500,MessageUtil.CONTRACTDEVICEISNULL);

        Contract contract = contractDao.selectByPrimaryKey(contractDevice.getContractId());
        if(contract == null)
            throw new ExceptionUtil(500,MessageUtil.CONTRACTNOEXIST);

        DeviceView device = new DeviceView();
        device.setDeviceId(installView.getDeviceId());
        device.setDeviceName(contractDevice.getDeviceName());
        device.setDeviceModel(contractDevice.getDeviceModel());
        device.setPlaceOrigin(contractDevice.getPlaceOrigin());
        device.setQuantity(1);
        device.setSpecifications(contractDevice.getSpecifications());
        device.setPrice(contractDevice.getPrice());
        device.setAttachment(contractDevice.getAttachment());
        device.setUpkeepCycle(contractDevice.getUpkeepCycle());
        device.setSn(installView.getSn());
        device.setDepartment(installView.getDepartment());
        device.setAssetsNo(installView.getAssetsNo());
        device.setInstallDate(installView.getActualInstallDate());
        device.setAcceptanceDate(format);
        device.setPurchaseDate(contract.getSignDate());
        device.setManufactureDate(contractDeviceActualSend.getActualSendDate());
        device.setDeviceStatus(1500L);
        device.setManufacturerId(contract.getSecondParty());
        device.setMaintainerId(contractDevice.getMaintainerId());
        device.setHospitalId(contract.getFirstParty());
        device.setContractDeviceId(contractDevice.getContractDeviceId());
        device.setCreatePerson(user.getUserId());
        device.setEnableTime(installView.getEnableTime());
        device.setIsDel(1);
        ReturnResultUtil resultUtil = deviceService.addOrUpdateDevice(device);
        if(resultUtil.getRet() != 200)
            throw new ExceptionUtil(500,resultUtil.getMsg(),device);

        installView.setDeviceId(device.getDeviceId());
        row = installDao.updateByPrimaryKeySelective(installView);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INFOCOMPLETIONERROR);

        //添加设备安装事件
        event.setEventId(UUIDGenerator.getUniqueLong());
        event.setKeyType(1603L);        //设备
        event.setKeyId(device.getDeviceId());
        event.setEventTitle("设备安装");
        event.setEventContent("设备于"+format+"验收安装");
        event.setEventDateTime(format);
        event.setType(1702L);   //设备安装	设备事件类型
        row = eventDao.insertSelective(event);
        if(row == 0)
            throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);
        return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL);
    }

    /**
     * 安装确认
     * @param install
     * @return
     */
    @Override
    @Transactional
    public ReturnResultUtil confirm(InstallView install){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());

        install.setCommentStatus(3501L);                    //已评论(已评论:3501,未评论:3502,已回复:3503)
        int result = installDao.updateByPrimaryKeySelective(install);
        if(result>0){
            Long deviceId = install.getDeviceId();  //实际设备ID
            Device device = deviceDao.selectByPrimaryKey(deviceId);
            if(device == null)
                throw new ExceptionUtil(500,MessageUtil.DEVICEISNULL);
            Long status = install.getStatus();

            Event event = new Event();
            event.setEventId(UUIDGenerator.getUniqueLong());
            event.setKeyId(install.getInstallId());
            event.setKeyType(1600L);
            event.setEventDateTime(format);
            event.setType(2402L);
            event.setCreatePerson(install.getUserId());
            event.setEventTitle("医院验收通过");
            event.setEventContent("医院于"+format+"验收通过并评分");
            //验收未通过
            if(status.intValue() == 1204){
                device.setDeviceStatus(1503L);
                event.setEventTitle("医院验收不通过");
                event.setEventContent("医院于"+format+"验收不通过，待平台联系医院");
            }
            //验收通过
            else if(status.intValue() == 1203){
                device.setDeviceStatus(1500L);
                device.setIsDel(0);
            }
            int i = deviceDao.updateByPrimaryKeySelective(device);
            if(i == 0)
                throw new ExceptionUtil(500,MessageUtil.INITDEVICEFAILED,device);
            i = eventDao.insertSelective(event);
            if(i == 0)
                throw new ExceptionUtil(500,MessageUtil.INSERTEVENTERROR,event);
            return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL);
        }
        return new ReturnResultUtil(500,MessageUtil.CONFIRMFAIL);
    }

    @Override
    public ReturnResultUtil installDispatch(ParameterView parameterView) {
        String projectChargePerson = parameterView.getProjectChargePerson();
        String projectChargePersonPhone = parameterView.getProjectChargePersonPhone();

        User userParameter = new User();
        userParameter.setMobile(projectChargePersonPhone);
        userParameter.setUserName(projectChargePersonPhone);
        User user = userDao.checkUserByEngineerPhone(userParameter);
        if (user == null) {
            user = new User();
            user.setUserId(UUIDGenerator.getUniqueLong());
            user.setUserName(projectChargePersonPhone);
            user.setMobile(projectChargePersonPhone);
            user.setRealName(projectChargePerson);
            user.setType(1103L);
            user.setTag(1006L);
            user.setMaintainerId(parameterView.getMaintainerId());
            user.setRemark("安装派工产生用户");
            int row5 = userDao.insertSelective(user);
            if (row5 == 0)
                return new ReturnResultUtil(500, MessageUtil.USERERROR, user);
        }
        Long installId = parameterView.getInstallId();
        //根据安装单ID查询发货单信息
        ContractActualSend contractActualSend = contractActualSendDao.queryByInstallId(installId);
        if(contractActualSend == null)
            throw new ExceptionUtil(500,MessageUtil.CONTRACTESTIMATESENDNOEXIST);
        //绑定发货单的工程师
        contractActualSend.setProjectChargePersonId(user.getUserId());
        int i = contractActualSendDao.updateByPrimaryKeySelective(contractActualSend);
        if(i == 0)
            throw new ExceptionUtil(500,MessageUtil.ACTUALINSERT);

        //根据发货单ID修改安装单信息
        Long contractActualSendId = contractActualSend.getContractActualSendId();
        InstallView installView = new InstallView();
        installView.setProjectChargePerson(projectChargePerson);
        installView.setProjectChargePersonPhone(projectChargePersonPhone);
        installView.setEstimateInstallDate(parameterView.getEstimateInstallDate());
        installView.setDispatchRemark(parameterView.getDispatchRemark());
        installView.setContractActualSendId(contractActualSendId);
        i = installDao.updateByContractActualSendId(installView);
        if(i == 0)
            throw  new ExceptionUtil(500,MessageUtil.INSTALLDISPATCHERROR);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        //指派记录
        Appoint appoint = new Appoint();
        appoint.setAppointId(UUIDGenerator.getUniqueLong());
        appoint.setKeyId(contractActualSendId);
        appoint.setKeyType(2903L);  //发货安装
        appoint.setInstructId(parameterView.getManufacturerId());
        appoint.setInstructType(1103L);
        appoint.setCreatePerson(parameterView.getUserId());
        appoint.setInstructedType(1103L);
        appoint.setInstructedId(parameterView.getManufacturerId());
        appoint.setCreateTime(format);
        appoint.setEngineerName(projectChargePerson);
        appoint.setEngineerPhone(projectChargePersonPhone);
        //回写上条指派记录的响应时间
        AppointView appointView = new AppointView();
        appointView.setKeyId(contractActualSendId);
        Appoint appoint1 = appointDao.queryLastAppoint(appointView);
        if(appoint1 == null)
            throw new ExceptionUtil(500,MessageUtil.APPOINTERROR);
        appoint1.setResponseTime(format);
        i = appointDao.updateByPrimaryKeySelective(appoint1);
        if(i == 0)
            throw new ExceptionUtil(500,MessageUtil.APPOINTERROR,appoint);

        i = appointDao.insertSelective(appoint);
        if(i == 0)
            throw new ExceptionUtil(500,MessageUtil.APPOINTERROR,appoint);

        return new ReturnResultUtil(200,MessageUtil.SUCCESSFUL);
    }
}
